package com.example.bestudentsystem.demos.web.controller;


import com.example.bestudentsystem.demos.web.common.Result;
import com.example.bestudentsystem.demos.web.mapper.AdminMapper;
import com.example.bestudentsystem.demos.web.mapper.CourseMapper;
import com.example.bestudentsystem.demos.web.mapper.StudentMapper;
import com.example.bestudentsystem.demos.web.mapper.TeacherMapper;
import com.example.bestudentsystem.demos.web.pojo.Admin;
import com.example.bestudentsystem.demos.web.pojo.Student;
import com.example.bestudentsystem.demos.web.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@CrossOrigin
public class myController {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    CourseMapper courseMapper;

    @GetMapping("/login")
    public Result login(@RequestParam String id, @RequestParam String password) {
        Admin admin1 = adminMapper.selectById(id);
        if (admin1 != null && admin1.getPassword().equals(password) && admin1.getId().equals(id)){
            return Result.success(admin1.getLevel());
        }

        Teacher teacher = teacherMapper.selectById(id);
        if (teacher != null && teacher.getPassword().equals(password) && teacher.getTno().equals(id)){
            return Result.success(teacher.getLevel());
        }

        Student student = studentMapper.selectById(id);
        if (student != null && student.getPassword().equals(password) && student.getSno().equals(id)){
            return Result.success(student.getLevel());
        }

        return Result.error();
    }
    @GetMapping("/register")
    public Result register(@RequestParam String id, @RequestParam String password) {
        Admin admin = adminMapper.selectById(id);
        if(admin!=null && admin.getPassword()==null ){
            admin.setPassword(password);
            adminMapper.updateById(admin);
            return Result.success(admin.getLevel());
        }

        Teacher teacher = teacherMapper.selectById(id);
        if(teacher!=null && teacher.getPassword()==null){
            teacher.setPassword(password);
            teacherMapper.updateById(teacher);
            return Result.success(teacher.getLevel());
        }

        Student student = studentMapper.selectById(id);
        if(student!=null && student.getPassword()==null){

            student.setPassword(password);
            studentMapper.updateById(student);
            return Result.success(student.getLevel());
        }

        return Result.error();
    }
    @GetMapping("/reset")
    public Result resetPass(@RequestParam String id, @RequestParam String newPass) {
        Admin admin = adminMapper.selectById(id);

        if(admin!=null&&admin.getPassword()!=null){
            admin.setPassword(newPass);
            adminMapper.updateById(admin);
            return Result.success(admin.getLevel());
        }

        Teacher teacher = teacherMapper.selectById(id);
        if(teacher!=null&&teacher.getPassword()!=null){
            teacher.setPassword(newPass);
            teacherMapper.updateById(teacher);
            return Result.success(teacher.getLevel());
        }

        Student student = studentMapper.selectById(id);
        if(student!=null&&student.getPassword()!=null){
            student.setPassword(newPass);
            studentMapper.updateById(student);
            return Result.success(student.getLevel());
        }

        return Result.error();
    }
}